package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import sun.awt.geom.AreaOp;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：zhang
 * @date ：Created in 2021/1/12
 * @description ：
 * @version: 1.0
 */
@DubboService
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReprotData() {


        Calendar today = Calendar.getInstance();
        // 时分秒清0
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND,0);
        today.set(Calendar.MILLISECOND,0);

        // 本周 第1天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.setFirstDayOfWeek(Calendar.MONDAY);// 设置每周第1天为星期一
        weekFirstDay.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        // 本周 最后1天
        Calendar weekLastDay = (Calendar) today.clone();
        weekLastDay.setFirstDayOfWeek(Calendar.MONDAY);// 设置每周第1天为星期一
        weekLastDay.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);

        // 本月第1天
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH,1);
        // 本月最后1天
        Calendar monthLastDay = (Calendar) today.clone();
        int actualMaximum = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月实际的日期最大值
        monthLastDay.set(Calendar.DAY_OF_MONTH,actualMaximum);


        Map<String,Object> result = new HashMap<>();
     //       reportDate -> yyyy-MM-dd        //日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        result.put("reportDate", df.format(new Date()));
    //       todayNewMember -> number		// 今日新增会员  today  today
        result.put("todayNewMember", memberDao.countByRegTimeRange(today.getTime(),today.getTime()));
        //       thisWeekNewMember -> number		// 本周新增会员  weekFirstDay   weekLastDay
        result.put("thisWeekNewMember",memberDao.countByRegTimeRange(weekFirstDay.getTime(),weekLastDay.getTime()));
        //       thisMonthNewMember -> number	// 本月新增会员  monthFirstDay     monthLastDay
        result.put("thisMonthNewMember",memberDao.countByRegTimeRange(monthFirstDay.getTime(),monthLastDay.getTime()));
    //       totalMember -> number			//  总会员
        result.put("totalMember",memberDao.countByRegTimeBefore(df.format(today.getTime())));





    //       todayOrderNumber -> number		// 今日预约人数
        result.put("todayOrderNumber",orderDao.countByOrderDateRange(today.getTime(),today.getTime(),null));
    //       thisWeekOrderNumber -> number	// 本周预约人数
        result.put("thisWeekOrderNumber",orderDao.countByOrderDateRange(weekFirstDay.getTime(),weekLastDay.getTime(),null));
    //       thisMonthOrderNumber -> number	// 本月预约人数
        result.put("thisMonthOrderNumber",orderDao.countByOrderDateRange(monthFirstDay.getTime(),monthLastDay.getTime(),null));

    //       todayVisitsNumber -> number		// 今日到诊人数
        result.put("todayVisitsNumber",orderDao.countByOrderDateRange(today.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
    //       thisWeekVisitsNumber -> number	// 本周到诊人数
        result.put("thisWeekVisitsNumber",orderDao.countByOrderDateRange(weekFirstDay.getTime(),weekLastDay.getTime(),Order.ORDERSTATUS_YES));
    //       thisMonthVisitsNumber -> number	// 本月到诊人数
        result.put("thisMonthVisitsNumber",orderDao.countByOrderDateRange(monthFirstDay.getTime(),monthLastDay.getTime(),Order.ORDERSTATUS_YES));




    //       hotSetmeal ->  [ {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5}]	// 热门套餐
        result.put("hotSetmeal", orderDao.countByHotSetmeal(4));

        return result;
    }
}
